
// Returns the value of the URL parameter keyed to 'name', or an empty string if no such parameter.
function getParameterByName(name) {
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
	var results = regex.exec(window.location.search);
	if (results == null)
		return "";
	else
		return decodeURIComponent(results[1].replace(/\+/g, " "));
}

// Convenience method for opening a floating IFrame dialog.
function Dialog(args) {
	if (typeof args['url'] == 'undefined'
		|| typeof args['width'] == 'undefined'
		|| typeof args['height'] == 'undefined'
		|| typeof args['name'] == 'undefined'
	) {
		return false
	}
	dialog.open(args);
}
function InitializeTrains(){
	Dialog({
		title: 'Initialize Trains',
		url: '/rwt/admin/initialize_trains.jsp',
		name: 'initialize_trains',
		width:550,
		height:180
	});
}
function CameraDisplay(){
	Dialog({
		title: 'Camera Display',
		url: '/rwt/camera_display.html',
		name: 'camera_display',
		width:300,
		height:220
	});
}

function SimpleSwitch(){
	var request = new OpenSwitchRequest(false, SWITCH_SIMPLE);
	window.top.conn.send(request);
}
function openSimpleSwitch() {
	Dialog({
		title: 'Simple Switch Display',
		url: '/rwt/simple_switch.jsp',
		name: 'simple_switch',
		width:450,
		height:330,
		onclose: function() {
			var msg = new CloseSwitchesRequest();
			window.top.conn.send(msg);
		}
	});
}

function VisualSwitch(){
	var request = new OpenSwitchRequest(false, SWITCH_VISUAL);
	window.top.conn.send(request);
}
function openVisualSwitch() {
	Dialog({
		title: 'Visual Switch Display',
		url: '/rwt/visual_switch.jsp',
		name: 'visual_switch',
		width:460,
		height:344,
		onclose: function() {
			var msg = new CloseSwitchesRequest();
			window.top.conn.send(msg);
		}
	});
}

function BasicThrottle(train_id){
	var request = new OpenThrottleRequest(train_id, false);
	window.top.conn.send(request);
}
function openBasicThrottle(train_id, virtualAddress) {
	Dialog({
		title: 'Train ' + train_id + ' (Virtual ' + virtualAddress + ')',
		url: '/rwt/basic_throttle.html?train_id='+train_id,
		name: 'basic_throttle_' + train_id,
		width:200,
		height:350,
		onclose: function() {
			var closeThrottleMsg = new CloseThrottle(train_id);
			window.top.conn.send(closeThrottleMsg);
		}
	});
}

function EmergencyThrottle(train_id){
	var request = new OpenThrottleRequest(train_id, true); // true to open with direct control
	window.top.conn.send(request);
}
function openEmergencyThrottle(train_id, virtualAddress){
	Dialog({
		title: 'Train ' + train_id + ' (Phys slot ' + virtualAddress + ')' + " [Emergency]",
		url: '/rwt/admin/emergency_throttle.html?train_id='+train_id,
		name: 'emergency_throttle_' + train_id,
		width:200,
		height:290,
		onclose: function() {
			var closeThrottleMsg = new CloseThrottle(train_id);
			window.top.conn.send(closeThrottleMsg);
		}
	});
}

function EmergencySwitch(){
	var request = new OpenSwitchRequest(true, SWITCH_EMERGENCY);
	window.top.conn.send(request);
}
function openEmergencySwitch() {
	Dialog({
		title: 'Emergency Switch Display',
		url: '/rwt/admin/emergency_switch.jsp',
		name: 'emergency_switch',
		width:450,
		height:330,
		onclose: function() {
			var msg = new CloseSwitchesRequest();
			window.top.conn.send(msg);
		}
	});
}
var dragvars = {
	elem: null,
	s_x: 0,
	s_y: 0,
	o_x: 0,
	o_y: 0,
	d: 10
};
var dialog = {
	open: function(args){
		if (typeof args['url'] == 'undefined') {
			return false;
		}
		if (typeof args['title'] == 'undefined') {
			args.title = 'New Dialog';
		}
		if (typeof args['width'] == 'undefined'){
			args.width = 400;
		}
		if (typeof args['height'] == 'undefined') {
			args.height = 400;
		}
		
		var con = document.createElement('div');
		con.setAttribute('class', 'm_dialog');
		con.setAttribute('name', args.name);
		con.style.zIndex = dragvars.d;
		dragvars.d++;
		
		var title = document.createElement('div');
		title.innerHTML = args.title;
		title.setAttribute('class', 'title');
		title.onmousedown = function(e){
			if (!this.getAttribute('type')) {
				dragvars.elem = this.parentNode;
				dragvars.s_x = e.pageX;
				dragvars.s_y = e.pageY;
				dragvars.o_x = dragvars.elem.offsetLeft;
				dragvars.o_y = dragvars.elem.offsetTop;
				dragvars.elem.style.zIndex = dragvars.d;
				dragvars.d++;
			}
			window.onmousemove = function(e){
				var new_x = dragvars.o_x+(e.pageX-dragvars.s_x);
				dragvars.elem.style.left = new_x+'px';
				var new_y = dragvars.o_y+(e.pageY-dragvars.s_y);
				dragvars.elem.style.top = new_y+'px';
				dragvars.elem.childNodes[1].style.visibility = 'hidden';
				return false;
			};
			window.onmouseup = function(e){
				window.onmousemove = null;
				window.onmouseup = null;
				window.onmousedown = null;
				dragvars.elem.childNodes[1].style.visibility = 'visible';
				
				return false;
			};
			return false;
		};
		con.appendChild(title);
		
		var close = document.createElement('input');
		close.setAttribute('type','button');
		close.setAttribute('value','Close');
		close.onclick = function(){
			this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
			if (typeof args['onclose'] != 'undefined') {
				args.onclose();
			}
		};
		title.appendChild(close);
		con.close_window = close.onclick;
		
		var content = document.createElement('div');
		content.setAttribute('class','content');
		content.setAttribute('style', 'width:'+args.width+'px;height:'+args.height+'px');
		con.appendChild(content);
		
		var frame = document.createElement('iframe');
		frame.setAttribute('name', 'if_' + args.name);
		frame.setAttribute('src', args.url);
		frame.setAttribute('allowtransparency',"yes");
		content.appendChild(frame);
		document.body.appendChild(con);
	}
};
